package com.example.softwareconfigurationcoursedesign;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AdminServlet   {

    // 数据库连接信息，请根据你的实际情况进行修改
    private static final String DB_URL = "jdbc:mysql://localhost:3306/course_design";
    private static final String DB_USERNAME = "root";
    private static final String DB_PASSWORD = "123456";

    // 获取数据库连接
    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
    }

    // 从数据库中获取会员列表
    public List<User> getUserListFromDatabase() {
        List<User> userList = new ArrayList<>();

        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement("SELECT * FROM users");
             ResultSet resultSet = statement.executeQuery()) {

            while (resultSet.next()) {
                String userId = resultSet.getString("uid");
                String name = resultSet.getString("name");
                String phoneNumber = resultSet.getString("phone_number");
                String password = resultSet.getString("password");

                User user = new User(1, name, phoneNumber, password);
                userList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace(System.err);
        }

        return userList;
    }

    // 添加会员到数据库
    public void addUserToDatabase(User user) {
        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement(
                     "INSERT INTO users (name, phone_number, password) VALUES (?, ?, ?)")) {

            statement.setString(1, user.getName());
            statement.setString(2, user.getPhoneNumber());
            statement.setString(3, user.getPassword());

            statement.executeUpdate();
            System.out.println("增加会员");
        } catch (SQLException e) {
            e.printStackTrace(System.err);
        }
    }

    // 编辑会员信息
    public void editUserInDatabase(User user) {
        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement(
                     "UPDATE users SET name=?, phone_number=?, password=? WHERE uid=?")) {

            statement.setString(1, user.getName());
            statement.setString(2, user.getPhoneNumber());
            statement.setString(3, user.getPassword());
            statement.setInt(4, user.getUserId());

            statement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace(System.err);
        }finally {
            System.out.println("编辑会员");
        }
    }

    // 从数据库中删除会员
    public void deleteUserFromDatabase(int userId) {
        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement(
                     "DELETE FROM users WHERE uid=?")) {

            statement.setInt(1, userId);

            statement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace(System.err);
        }finally {
            System.out.println("删除会员");
        }
    }

}
